//
//  FourViewController.m
//  UI_豆瓣影院
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "FourViewController.h"
#import "DlViewController.h"
#import "DyViewController.h"
#import "AAALHXDateHandle.h"
#import "MovieViewController.h"
@interface FourViewController ()<UITableViewDataSource, UITableViewDelegate,Delegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic,retain) UIBarButtonItem *item;
@end

@implementation FourViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bg_nav.png"] forBarMetrics:UIBarMetricsDefault];
    }
    return self;
}

- (void)dealloc {
    [_tableView release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667) style:UITableViewStylePlain];
    self.navigationItem.title = @"我的";
   NSArray *arr = [[AAALHXDateHandle sharedDateBaseHandle] selectTable2];
    myModel *model = [myModel model];
    model = [arr lastObject];
    if (model.type == 1) {
        model.name = @"登陆";
    } else {
        
        model.name = @"注销";
    }
    self.navigationController.navigationBar.translucent = NO;
    self.item = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    _item.tag = 1000;
    self.navigationItem.rightBarButtonItem = _item;
    [_item setTintColor:[UIColor blackColor]];

    
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.separatorColor = [UIColor brownColor];
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableView release];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 120)];
    imgView.image = [UIImage imageNamed:@"IMG_5.JPG"];
    _tableView.tableHeaderView = imgView;
    
}


// 协议传值
- (void)backTo {
    
    _item.title = @"注销";
    
}

// 注销 按钮
- (void)itemAction1 {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认注销" preferredStyle:1];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
        [item setTintColor:[UIColor blackColor]];
        self.navigationItem.rightBarButtonItem = item;
    }];
    
    
    [alert addAction:action];
    [alert addAction:action1];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
    
}

- (void)itemAction:(UIBarButtonItem *)item {
   NSArray *arr = [[AAALHXDateHandle sharedDateBaseHandle] selectTable2];
    myModel *model = [myModel model];
    model = [arr lastObject];
    
    if ([_item.title isEqualToString:@"登陆"]) {

        model.type = 0;
        [[AAALHXDateHandle sharedDateBaseHandle] updataTable:model];
        
        DlViewController *vc = [[DlViewController alloc] init];
        UINavigationController *nav  = [[UINavigationController alloc] initWithRootViewController:vc];
        nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:nav animated:YES completion:^{
            
        }];
        vc.myDelegate = self;
    } else {
        
        NSInteger i = 1;
        model.type = i;
        [[AAALHXDateHandle sharedDateBaseHandle] updataTable:model];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认注销" preferredStyle:1];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            _item.title = @"登陆";
        }];
        
        
        [alert addAction:action];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
        
        
        
        
    }
}

// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
    
}
// 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
//  编辑
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (indexPath.row == 0) {
        cell.textLabel.font = [UIFont systemFontOfSize:22];
        cell.textLabel.text = @"我的活动";
        return cell;
    } else if (indexPath.row == 1) {
        cell.textLabel.font = [UIFont systemFontOfSize:22];
        cell.textLabel.text = @"我的电影";
        return cell;
    } else if (indexPath.row == 2) {
        cell.textLabel.font = [UIFont systemFontOfSize:22];
        cell.textLabel.text = @"清除缓存";
        return cell;
    }
    return cell;
}

// 点击方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        if ([_item.title isEqualToString:@"登陆"]) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录" preferredStyle:(UIAlertControllerStyleAlert)];
            [self presentViewController:alert animated:YES completion:nil];
            [self performSelector:@selector(time) withObject:alert afterDelay:1];
            
        } else {
            
            DyViewController *vc = [[DyViewController alloc] init];
            vc.arr = [[AAALHXDateHandle sharedDateBaseHandle] selectTable];
            [self.navigationController pushViewController:vc animated:YES];
        }
    } else if (indexPath.row == 1) {
        if ([_item.title isEqualToString:@"登陆"]) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录" preferredStyle:(UIAlertControllerStyleAlert)];
            [self presentViewController:alert animated:YES completion:nil];
            [self dismissViewControllerAnimated:YES completion:nil];
            
        } else {
            
            MovieViewController *vc = [[MovieViewController alloc] init];
            vc.arr = [[AAALHXDateHandle sharedDateBaseHandle] selectTable1];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    } else if (indexPath.row == 2) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否清空" preferredStyle:(UIAlertControllerStyleActionSheet)];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            

            [[AAALHXDateHandle sharedDateBaseHandle] deleteALLModel];
        
        }];
        [alert addAction:action];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:nil];
        
        
    }
    
    
    
    
}

- (void)time {
    [self dismissViewControllerAnimated:YES completion:nil];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
